﻿function LogOnModel() {
    this.Username = ko.observable("");
    this.Password = ko.observable("");
    this.RememberMe = ko.observable(false);
}

function RegisterModel() {
    this.Email = ko.observable("").extend({
        required: true,
        maxLength: 50,
        email: true
    });
    this.Username = ko.observable("").extend({
        required: true,
        maxLength: 20
    });
    this.Password = ko.observable("").extend({
        required: true,
        minLength: 8
    });
    this.ConfirmPassword = ko.observable("").extend({
        required: true,
        minLength: 8,
        equal: this.Password
    });
    this.recaptcha_challenge_field = ko.observable("");
    this.recaptcha_response_field = ko.observable("");
    this.errors = ko.validation.group(this);
    return;
}

function LogOnViewModel(parent) {
    var self = this;
    self.logOnModel = ko.observable();
    self.selectLogOnModel = function() {
        self.logOnModel(new LogOnModel({}));
        self.signInMessageVisible(false);
    };
    self.registerModel = ko.observable();
    self.selectRegisterModel = function() {
        self.registerModel(new RegisterModel({}));
        self.captchaErrorMessageVisible(false);
        self.registerErrorMessageVisible(false);
    };
    self.signInMessage = ko.observable("");
    self.signInMessageVisible = ko.observable(false);

    self.captchaVisible = ko.observable(false);
    self.checkCaptchaVisible = ko.computed(function () {
        if (self.registerModel()) {
            if (self.registerModel().errors().length == 0) {
                self.captchaVisible(true);
            }
        }
    });
   
    self.signIn = function () {
        self.signInMessageVisible(false);
        $.ajax({
            url: "/Account/SignIn",
            type: 'post',
            data: { Username: self.logOnModel().Username, Password: self.logOnModel().Password, RememberMe: self.logOnModel().RememberMe },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            },
            success: function (result) {
                if (result.message == "Ok.") {
                    $("#SignInDialog").modal("hide");
                    window.location.reload();
                    parent.folder.loadFolders();
                }
                else {
                    self.signInMessage(result.message);
                    self.signInMessageVisible(true);
                }
            }
        });
    };
    self.captchaErrorMessageVisible = ko.observable(false);
    self.captchaErrorMessage = ko.observable("");
    self.registerErrorMessageVisible = ko.observable(false);
    self.registerErrorMessage = ko.observable("");
    self.register = function () {
        if (self.registerModel().errors().length == 0) {
            self.captchaErrorMessageVisible(false);
            self.registerErrorMessageVisible(false);
            self.registerModel().recaptcha_challenge_field($("#recaptcha_challenge_field").val());
            self.registerModel().recaptcha_response_field($("#recaptcha_response_field").val());
            $.ajax({
                url: "/Account/Register",
                type: 'post',
                data: self.registerModel(),
                success: function (result) {
                    if (result.message == "Ok.") {
                        self.sendEmail();
                        $("#RegisterDialog").modal("hide");
                        location.hash = "#/Confirmation";
                    }
                    else if (result.message == "Please type again, the two words bellow.") {
                        self.captchaErrorMessageVisible(true);
                        self.captchaErrorMessage(result.message);
                    }
                    else {
                        self.registerErrorMessageVisible(true);
                        self.registerErrorMessage(result.message);
                    }
                }
            });
        }
    };
    self.sendEmail = function () {
        $.ajax({
            url: "/Account/SendWelcomeMessage",
            type: 'post',
            data: self.registerModel()
        });
    };
    self.signOut = function () {
        $.ajax({
            url: "/Account/SignOut",
            type: 'post',
            success: function () {
                location.hash = "#/";
                window.location.reload();
            }
        });
    };
    self.cancelSignIn = function() {
        $("#SignInDialog").modal("hide");
    };
    self.cancelRegister = function() {
        $("#RegisterDialog").modal("hide");
    };

}